package elion.com.theElion.fragmentServer;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import elion.com.elion.R;
import elion.com.theElion.activityMain.ShowContentActivity;
import elion.com.theElion.util.LazyLoadFragment;
import elion.com.theElion.util.ReflectUtil;
import elion.com.theElion.util.UIHelper;
import elion.com.theElion.util.adapter.ServiceAdapter;
import elion.com.theElion.util.asynectask.ServiceAsy;

/**
 * Created by xiaon on 2017/6/9.
 */

public abstract class ServiceFragmentItem extends LazyLoadFragment {

//    protected Class aClass;

    private static final int TIME = 1000;

    protected View mView = getView();
    protected List<Map<String, String>> mList = new ArrayList<>();
    protected PullToRefreshListView pullToRefreshListView;
    protected ServiceAdapter adapter;
    protected ServiceAsy serviceFuWuAsy;
    protected int n = 0;
    protected String name[];
    protected Object object;

    protected callBack callBack;

    public ServiceFragmentItem() {
    }

    //  public abstract ServiceFragmentItem newInstance(String param1, String param2);

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//        mView = inflater.inflate(R.layout.fragment_service_item, container, false);
////        findView();
////        inits();
////        setting();
//        return mView;
//    }

//192.168.1.111

    public void inits() {

        this.callBack = getCallBack();
        this.object = setObject();
        name = ReflectUtil.getFieldsNames(object);
        intoAsy();

    }

    public void setPullToRefresh() {
        pullToRefreshListView.setScrollingWhileRefreshingEnabled(true);
        init();
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                System.out.println("下拉刷新");
                intoAsy();
            }
        });

        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ShowContentActivity.class);
                Bundle bundle = new Bundle();
                for (int i = 0; i < name.length; i++) {
                    bundle.putString(name[i], mList.get(position - 1).get(name[i]));
                }
                bundle.putStringArray("name", name);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    public void findView() {

        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.service_pull_refresh_list);
        setPullToRefresh();

    }

    public void intoAsy() {
        serviceFuWuAsy = new ServiceAsy(object, getActivity());
        serviceFuWuAsy.setCallBack(new ServiceAsy.callBack() {
            @Override
            public void setListview(List<Map<String, String>> list, String[] name) {
                pullToRefreshListView.onRefreshComplete();
                if (list == null) {
                } else {
                    mList.addAll(list);
                }

                adapter = new ServiceAdapter(getActivity(), mList, object);
                pullToRefreshListView.setAdapter(adapter);
                pullToRefreshListView.onRefreshComplete();
            }
        });
        serviceFuWuAsy.execute(callBack.setAsyPath());
        new UIHelper().hideDialogForLoading();

    }

    private void init() {
        ILoadingLayout startLabels = pullToRefreshListView
                .getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新...");// 刚下拉时，显示的提示
        startLabels.setRefreshingLabel("正在载入...");// 刷新时
        startLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示

        ILoadingLayout endLabels = pullToRefreshListView.getLoadingLayoutProxy(
                false, true);
        endLabels.setPullLabel("上拉刷新...");// 刚下拉时，显示的提示
        endLabels.setRefreshingLabel("正在载入...");// 刷新时
        endLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示
    }

    /**
     * @return 返回子类中 对象 的模板，以便得到 参数名
     */
    public abstract Object setObject();

    /**
     * 子类中设置
     */
    public abstract void setting();

    public abstract callBack getCallBack();

    protected interface callBack {

        String setAsyPath();

        // Objects setMyObject();

    }


    @Override
    protected int setContentView() {
        return R.layout.fragment_service_item;
    }

    @Override
    protected void lazyLoad() {
        findView();
        inits();
        setting();
    }
}
